package com.ejb;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;

/**
 * @desc 描述
 * create on 2018/10/31 by watson
 */
public class SayHelloClient {
    public static void main(String[] args) {
        try {
            /*final Hashtable jndiProperties = new Hashtable();
            jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
            jndiProperties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
            final Context context = new InitialContext(jndiProperties);*/


            Hashtable map = new Hashtable();
            map.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
            map.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
            map.put("java.naming.provider.url", "localhost");

            //相当于beanfactory 实例化封装的类
            InitialContext ic = new InitialContext(map);
            /**
                   * 实例InitialContext有两种方式
                   * 1、将属性值放到Hashtable中，传进去new InitialContext(map);
                   * 2、在src下创建jndi.properties文件，将属性保存在Propertis文件中 ,直接 new InitialContext();即可
                   */

            //得到远程的MyEjbImpl类，MyEjbImp/remote是服务器中MyEjb和MyEjbImpl的发布的Jndi名称
//            MyEjb tg =(MyEjb) ic.lookup("MyEjbImpl/remote");
            HelloWorld client =(HelloWorld) ic.lookup("HelloWorldBean/remote");
//            HelloWorld client =(HelloWorld) context.lookup("HelloWorldBean/remote");

            String ss = client.sayHello("") ; //远程调用EJB服务端的方法

            System.out.println("====="+ss);

        } catch (NamingException e) {
            e.printStackTrace();
        }
    }


}

